/*
 * @Author: jingzi 1163478123@qq.com
 * @Date: 2025-05-29 16:38:59
 * @LastEditors: jingzi 1163478123@qq.com
 * @LastEditTime: 2025-11-08 16:45:42
 * @Description: 文件描述
 * Copyright (c) 2025 by ${git_name}, All Rights Reserved.
 */
import { Toast } from './toast.js'
export const copyToClipboard = async (text) => {
    try {
      await navigator.clipboard.writeText(text)
      Toast.success(`${text} 复制成功！`)
    } catch (err) {
      const textarea = document.createElement('textarea')
      textarea.value = text
      document.body.appendChild(textarea)
      textarea.select()
      try {
        document.execCommand('copy')
        Toast.success(`${text} 复制成功！`)
      } catch (err2) {
        Toast.error('复制失败')
      }
      document.body.removeChild(textarea)
    }
  }
/**
 * @description: 设置缓存信息
 * @param {string} name 缓存名称
 * @param {any} data 缓存数据
 */
export const setStorageData = (name, data) => {
  return new Promise((resolve,reject)=>{
    chrome.storage.local.set({ [name]: data }, () => {
      resolve(data)
    });
  })
}
/**
 * @description: 获取缓存信息
 * @param {string} name 缓存名称
 * @returns {Promise<any>} 缓存信息
 */
export const getStorageData = (name) =>{
  return new Promise((resolve,reject)=>{
    chrome.storage.local.get(name,(result)=>{
      // 当前无数据返回{}，对此数据做处理
      if(result[name]){
        resolve(result[name])
      }else{
        resolve([])
      }
    })
  })
}
/**
 * @description: 获取缓存信息
 * @param {string} name 缓存名称，可以是字符串或者数组
 * @returns {Promise<any>} 缓存信息
 */
export const removeStorageData = (name) =>{
  return new Promise((resolve,reject)=>{
    chrome.storage.local.remove(name,()=>{
      resolve(`${name}删除成功`)
    })
  })
}